package com.bootdo.lvyou_specialty.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.utils.*;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyLunbo;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyMenu;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyMerchant;
import com.bootdo.lvyou_specialty.service.LvyouSpecialtyMenuService;
import com.bootdo.lvyou_specialty.service.LvyouSpecialtyMerchantService;
import com.bootdo.system.dao.UserDao;
import com.bootdo.system.dao.UserRoleDao;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyProduct;
import com.bootdo.lvyou_specialty.service.LvyouSpecialtyProductService;

/**
 * 
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-10-10 11:41:24
 */
 
@Controller
@RequestMapping("/lvyou_specialty/lvyouSpecialtyProduct")
public class LvyouSpecialtyProductController extends BaseController{


	@Autowired
	private LvyouSpecialtyProductService lvyouSpecialtyProductService;

	@Autowired
	private LvyouSpecialtyMenuService lvyouSpecialtyMenuService;

	@Autowired
	private LvyouSpecialtyMerchantService lvyouSpecialtyMerchantService;

	@Autowired
	private UserRoleDao userRoleDao;
	
	@ModelAttribute
	public LvyouSpecialtyProduct get(@RequestParam(required=false) Long id) {
		if (id != null){
			return lvyouSpecialtyProductService.get(id);
		}else{
			return new LvyouSpecialtyProduct();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/lvyou_specialty/lvyouSpecialtyProduct','lvyou_specialty:lvyouSpecialtyProduct:lvyouSpecialtyProduct',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','lvyou_specialty:lvyouSpecialtyProduct:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:lvyouSpecialtyProduct")	
	String LvyouSpecialtyProduct(LvyouSpecialtyProduct lvyouSpecialtyProduct,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou_specialty/lvyouSpecialtyProduct/lvyouSpecialtyProduct";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:lvyouSpecialtyProduct")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<LvyouSpecialtyProduct> lvyouSpecialtyProductList = lvyouSpecialtyProductService.list(query);
		int total = lvyouSpecialtyProductService.count(query);
		PageUtils pageUtils = new PageUtils(lvyouSpecialtyProductList, total);
		return R.ok().put("rows", lvyouSpecialtyProductList).put("total", total);
	}

	// 该方法是为了向供应商部分进行显示商品
	@ResponseBody
	@PostMapping("/listGongying")
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:lvyouSpecialtyProduct")
	public R listGongying(@RequestParam Map<String, Object> params){
		UserDO user = ShiroUtils.getUser();
		params.put("merchantId", user.getMerchantId());
		//查询列表数据
		Query query = new Query(params);
		List<LvyouSpecialtyProduct> lvyouSpecialtyProductList = lvyouSpecialtyProductService.list(query);
		int total = lvyouSpecialtyProductService.count(query);
		PageUtils pageUtils = new PageUtils(lvyouSpecialtyProductList, total);
		return R.ok().put("rows", lvyouSpecialtyProductList).put("total", total);
	}

	@ResponseBody
	@PostMapping("/listlv")
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:lvyouSpecialtyProduct")
	public R listlv(@RequestParam Map<String, Object> params){
		//查询列表数据
		params.put("pendingFlag", 3);
//		params.put("status", 1);
		Query query = new Query(params);
		List<LvyouSpecialtyProduct> lvyouSpecialtyProductList = lvyouSpecialtyProductService.list(query);
		int total = lvyouSpecialtyProductService.count(query);
		PageUtils pageUtils = new PageUtils(lvyouSpecialtyProductList, total);
		return R.ok().put("rows", lvyouSpecialtyProductList).put("total", total);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:edit")
	String add(LvyouSpecialtyProduct lvyouSpecialtyProduct,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou_specialty/lvyouSpecialtyProduct/lvyouSpecialtyProductedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(LvyouSpecialtyProduct lvyouSpecialtyProduct) {
		return R.ok().put("i",lvyouSpecialtyProduct);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:edit")
	public R save( LvyouSpecialtyProduct lvyouSpecialtyProduct){

		// 暂时没有会员价，只是先设计为会员价等于商品价
		lvyouSpecialtyProduct.setHprice(lvyouSpecialtyProduct.getPrice());
		lvyouSpecialtyProduct.setDescInfo(DocumentUtil.setWidth(lvyouSpecialtyProduct.getDescInfo()));

		// 权限判定：为了让旅游局可以自己添加商品
		UserDO user = ShiroUtils.getUser();
		List<Long> list = userRoleDao.listRoleId(user.getUserId());
		if (list.contains(8L) || list.contains(1L)) {
			if (lvyouSpecialtyProduct.getMerchantId() == null) {
				lvyouSpecialtyProduct.setPendingFlag(3);
				if(lvyouSpecialtyProductService.save(lvyouSpecialtyProduct)>0){
					return R.ok();
				} else {
					return R.error();
				}
			}
		}

		if (lvyouSpecialtyProduct.getMerchantId() != null) {
			LvyouSpecialtyMerchant lvyouSpecialtyMerchant = lvyouSpecialtyMerchantService.get(lvyouSpecialtyProduct.getMerchantId());
			if (lvyouSpecialtyMerchant == null) {
				return R.error();
			}
			lvyouSpecialtyProduct.setMerchantName(lvyouSpecialtyMerchant.getName());
		}


		if(lvyouSpecialtyProductService.save(lvyouSpecialtyProduct)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:edit")
	public R remove( Long id){
		if(lvyouSpecialtyProductService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		lvyouSpecialtyProductService.batchRemove(ids);
		return R.ok();
	}


	@PostMapping("/subCheck")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:edit")
	R subCheck(@RequestParam("id")Long id) {

		LvyouSpecialtyProduct lvyouSpecialtyProduct = lvyouSpecialtyProductService.get(id);
		if (lvyouSpecialtyProduct == null) {
			return R.error();
		}

		lvyouSpecialtyProduct.setPendingFlag(2);
		if (lvyouSpecialtyProductService.save(lvyouSpecialtyProduct) == 0) {
			return R.error();
		}

		return R.ok().put("i",lvyouSpecialtyProduct);
	}

	@PostMapping("/changType")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:editlv")
	R changType(@RequestParam("id")Long id, @RequestParam("typeId")Long typeId) {

		LvyouSpecialtyProduct lvyouSpecialtyProduct = lvyouSpecialtyProductService.get(id);
		LvyouSpecialtyMenu lvyouSpecialtyMenu = lvyouSpecialtyMenuService.get(typeId);

		if (null == lvyouSpecialtyMenu || null == lvyouSpecialtyProduct) {
			return R.error();
		}

		lvyouSpecialtyProduct.setTypeId(lvyouSpecialtyMenu.getId());
		lvyouSpecialtyProduct.setTypeName(lvyouSpecialtyMenu.getName());
		lvyouSpecialtyProduct.setPendingFlag(3);
		lvyouSpecialtyProduct.setStatus(1);
		if (lvyouSpecialtyProductService.save(lvyouSpecialtyProduct) == 0) {
			return R.error();
		}

		return R.ok().put("i", lvyouSpecialtyProduct);
	}

	@PostMapping("/getCheckNumber")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:editlv")
	R getCheckNumber() {

		Map<String, Object> param = new HashMap<>();
		param.put("pendingFlag", 2);
		int number = lvyouSpecialtyProductService.count(param);

		return R.ok().put("number", number);
	}

	@PostMapping("/getCheckProduct")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:editlv")
	R getCheckProduct() {

		Map<String, Object> param = new HashMap<>();
		param.put("pendingFlag", 2);
		List<LvyouSpecialtyProduct> list = lvyouSpecialtyProductService.list(param);
		int total = lvyouSpecialtyProductService.count(param);
		return R.ok().put("rows", list).put("total", total);
	}

	@PostMapping("/againstProduct")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:editlv")
	R againstProduct(@RequestParam("id")Long id) {

		LvyouSpecialtyProduct lvyouSpecialtyProduct = lvyouSpecialtyProductService.get(id);
		if (lvyouSpecialtyProduct == null) {
			return R.error();
		}

		lvyouSpecialtyProduct.setPendingFlag(4);
		if (lvyouSpecialtyProductService.save(lvyouSpecialtyProduct) == 0) {
			return R.error();
		}

		return R.ok().put("i", lvyouSpecialtyProduct);
	}

	@PostMapping("/soldOut")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:editlv")
	R soldOut(@RequestParam("id")Long id) {

		LvyouSpecialtyProduct lvyouSpecialtyProduct = lvyouSpecialtyProductService.get(id);
		if (lvyouSpecialtyProduct == null) {
			return R.error();
		}

		lvyouSpecialtyProduct.setStatus(2);
		if (lvyouSpecialtyProductService.save(lvyouSpecialtyProduct) == 0) {
			return R.error();
		}

		return R.ok().put("i", lvyouSpecialtyProduct);
	}

	@PostMapping("/soldUp")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:editlv")
	R soldUp(@RequestParam("id")Long id) {

		LvyouSpecialtyProduct lvyouSpecialtyProduct = lvyouSpecialtyProductService.get(id);
		if (lvyouSpecialtyProduct == null) {
			return R.error();
		}
		lvyouSpecialtyProduct.setStatus(1);
		if (lvyouSpecialtyProductService.save(lvyouSpecialtyProduct) == 0) {
			return R.error();
		}

		return R.ok().put("i", lvyouSpecialtyProduct);
	}

	@PostMapping("/addToLunbo")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:editlv")
	R addToLunbo(@RequestParam("goodsId")Long goodsId,
				 @RequestParam("typeId")Long typeId,
				 @RequestParam("image")String image,
				 @RequestParam("title")String title,
				 @RequestParam("type")Integer type) {
		LvyouSpecialtyProduct lvyouSpecialtyProduct = lvyouSpecialtyProductService.get(goodsId);
		if (lvyouSpecialtyProduct == null) {
			return R.error();
		}
		LvyouSpecialtyLunbo lvyouSpecialtyLunbo = new LvyouSpecialtyLunbo();
		lvyouSpecialtyLunbo.setGoodsId(goodsId);
		lvyouSpecialtyLunbo.setTitle(title);
		lvyouSpecialtyLunbo.setImage(image);
		lvyouSpecialtyLunbo.setType(type);
		lvyouSpecialtyLunbo.setTypeId(typeId);

		lvyouSpecialtyProduct.setIsLunbo(2);
		if (lvyouSpecialtyProductService.addToLunbo(lvyouSpecialtyProduct, lvyouSpecialtyLunbo) == 0) {
			return R.error();
		}

		return R.ok().put("i", lvyouSpecialtyProduct);
	}


	@PostMapping("/deleteFromLunbo")
	@ResponseBody
	@RequiresPermissions("lvyou_specialty:lvyouSpecialtyProduct:editlv")
	R deleteFromLunbo(@RequestParam("id")Long id) {
		LvyouSpecialtyProduct lvyouSpecialtyProduct = lvyouSpecialtyProductService.get(id);
		if (lvyouSpecialtyProduct == null) {
			return R.error();
		}

		lvyouSpecialtyProduct.setIsLunbo(1);

		if (lvyouSpecialtyProductService.deleteFromLunbo(lvyouSpecialtyProduct) == 0) {
			return R.error();
		}

		return R.ok().put("i", lvyouSpecialtyProduct);
	}
}